// Server-side Code

// Define actions which can be called from the client using ss.rpc('demo.ACTIONNAME', param1, param2...)
exports.actions = function(req, res, ss) {

  // Example of pre-loading sessions into req.session using internal middleware
  req.use('session');
  CommentsModel = require('./chatModel');

  // Uncomment line below to use the middleware defined in server/middleware/example
  //req.use('example.authenticated')

  return {

    addElement: function(key, element) {
      console.log('rpc. Key=' + key + ', ' + element);
      if (element && element.message && element.message.length > 0) {         // Check for blank messages
        ss.publish.all('elementAdded', key, CommentsModel.add(element) );     // Broadcast the message to everyone
        return res(true);                          // Confirm it was sent to the originating client
      } else {
        return res(false);
      }
    },

    elements: function(key) {
      console.log('rpc. Key=' + key);
      ss.publish.all('elements', key, CommentsModel.elements() );     // Broadcast the message to everyone
    },

  };

};